package org.gc.webapp.action.register;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.gc.Constants;
import org.gc.common.vo.security.UserVO;
import org.gc.exception.UserExistsException;
import org.gc.service.security.RoleService;
import org.gc.service.security.UserService;
import org.gc.webapp.action.BaseAction;
import org.springframework.security.userdetails.UsernameNotFoundException;



/**
 * Action to allow new users to sign up.
 */
public class RegisterAction extends BaseAction {
    private static final long serialVersionUID = 6558317334878272308L;

    private UserService userService;
    private RoleService roleService;
    
    private UserVO user;
    private Boolean isUsernameExist;
    
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public void setUser(UserVO user) {
        this.user = user;
    }

    /**
     * Return an instance of the user - to display when validation errors occur
     * @return a populated user
     */
    public UserVO getUser() {
        return user;
    }

    public Boolean getIsUsernameExist() {
        return isUsernameExist;
    }

    public void setIsUsernameExist(Boolean isUsernameExist) {
        this.isUsernameExist = isUsernameExist;
    }



    /**
     * Save the user, encrypting their passwords if necessary
     * @return success when good things happen
     * @throws Exception when bad things happen
     */
    @Action("register")
    public String register() throws Exception {
        user.setEnabled(true);

        // Set the default user role on this new user
        user.addRole(roleService.getRole(Constants.USER_ROLE));
        try{
            if(userService.getUserByUsername(user.getUsername()) != null){
                addFieldError("user.username", "用户名已存在。");
                return INPUT;
            }
        }catch(UsernameNotFoundException e){
        }
        try {
            userService.register(user);
        } catch (UserExistsException e) {
            log.warn(e.getMessage());
            addFieldError("user.email", "电子邮箱地址已存在(一个电子邮箱只能注册一个帐号)。");

            // redisplay the unencrypted passwords
            user.setPassword(user.getConfirmPassword());
            return INPUT;
        } catch(Exception e){
            log.warn(e);
            this.saveMessage("注册失败：" + e.getMessage());
            // redisplay the unencrypted passwords
            user.setPassword(user.getConfirmPassword());
            return INPUT;
        }

        getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE);

        // 注册完成自动将用户置为登录状态
        /*UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
                user, user.getConfirmPassword(), user.getAuthorities());
        auth.setDetails(user);
        SecurityContextHolder.getContext().setAuthentication(auth);*/

        return SUCCESS;
    }
    /*@Action(value = "isUserExist", results = { @Result(name = "success", type = "json") })
    public String isUserExist(){
        UserVO t = userService.getUserByUsername(user.getUsername());
        try{
            if(t != null){
                isUsernameExist = true;
            }else{
                isUsernameExist = false;
            }
        }catch(UsernameNotFoundException e){
            isUsernameExist = false;
        }
        return SUCCESS;
    }*/
}
